package com.projet.whattowatch.adapter;

import java.util.ArrayList;

import com.projet.whattowatch.R;
import com.projet.whattowatch.activity.ConnectedActivity;
import com.projet.whattowatch.activity.MainActivity;
import com.projet.whattowatch.activity.MovieDetailActivity;
import com.projet.whattowtach.utils.ImageLoader;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
 
public class MovieAdapter extends ArrayAdapter<String>
{
	private final Activity context;
	private final ArrayList<String> name;
	private final ArrayList<String> image;
	private ImageLoader imageLoader;
	
	final String EXTRA_MOVIE_NAME = "movie_name";
	
	public MovieAdapter(Activity context, ArrayList<String> name, ArrayList<String> image)
	{
		super(context, R.layout.custom_listview_plus, name);
		this.context = context;
		this.name = name;
		this.image = image;
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent)
	{
		LayoutInflater inflater = context.getLayoutInflater();
		View rootView = inflater.inflate(R.layout.custom_listview_plus, null);
		
		int loader = R.drawable.defaut_movie_img;
		Button imageView = (Button) rootView.findViewById(R.id.imagePerson);
		TextView namePerson = (TextView) rootView.findViewById(R.id.namePerson);
		Button addButton = (Button) rootView.findViewById(R.id.addButton);
		namePerson.setText(name.get(position));
		imageLoader = new ImageLoader(context);
		imageLoader.DisplayImage(image.get(position), loader, imageView);
		
		addButton.setOnClickListener(new OnClickListener()
		{
            @Override
            public void onClick(View v)
            {
            	for(int i = 0; i < MainActivity.mLibFilm.getListFilm().size(); i++)
            	{
            		if((MainActivity.mLibFilm.getListFilm().get(i).getNom()).equals(name.get(position)))
    				{
        				ConnectedActivity.mUtilisateur.addFilmAVoir(MainActivity.mLibFilm.getListFilm().get(i));
        				MainActivity.app_db.addFilmAVoir(ConnectedActivity.mUtilisateur, MainActivity.mLibFilm.getListFilm().get(i));
        				Toast.makeText(context, R.string.add_f_fav, Toast.LENGTH_SHORT).show();
    				}
            	}
            }
        });
		
		rootView.setOnClickListener(new OnClickListener()
		{
            @Override
            public void onClick(View v)
            {
            	String movieName = name.get(position);
		    	
		    	Intent movieDetail = new Intent(context, MovieDetailActivity.class);
		    	movieDetail.putExtra(EXTRA_MOVIE_NAME, movieName);
		    	context.startActivity(movieDetail);
            }
        });
		
		return rootView;
	}
}